import { observable, action } from 'mobx';
import * as loginApi from '~/apis/login/loginApi';
// import { loadingService } from '~/services/loading';

const state = observable({
  userInfo: null,
  loading: false,
});

// const getUser = action(() => {
//   const stop = loadingService.startLoading();
//   state.loading = true;
//   return loginApi.getUser().then((user) => {
//     state.user = user;
//   }).finally(() => {
//     state.loading = false;
//     stop();
//   });
// });

// const updateProfile = action((_p: any) => Promise.resolve());

const login = action((username: string, password: string) => loginApi.login({
  username,
  password,
}).then((res) => {
  // save userInfo
  state.userInfo = res.data;
}));

const logout = action(() => loginApi.logout().then(() => {
  state.userInfo = null;
}));

export const userService = {
  state,
  // getUser,
  // updateProfile,
  login,
  logout,
};
